// "use strict";
// import Vue from 'vue';
import axios from 'axios';
import Router from '../router/index';
import { MessageBox, Message } from 'element-ui';

axios.defaults.timeout = 600000;
axios.defaults.withCredentials = true;
// if (process.env.NODE_ENV == 'production') {
// axios.defaults.baseURL = process.env.VUE_APP_SRC;
// }
axios.defaults.headers['Content-Type'] = 'application/json; charset=utf-8';

const instance = axios.create();

// 请求发出拦截器
instance.interceptors.request.use(
    config => {
        // let loginToken = window.sessionStorage.getItem('token');
        // //每次发送请求之前检测都vuex存有token,那么都要放在请求头发送给服务器
        // if (loginToken) {
        //     // 让每个请求携带自定义token 请根据实际情况自行修改
        //     config.headers['Authorization'] = 'Bearer-' + loginToken;
        // }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

let sessionTipsCounter = 0;
// 请求接收数据拦截器
instance.interceptors.response.use(
    response => {
        // console.log(response, 'blob');
        if (response.config.responseType === 'blob') {
            return response.data;
        }
        switch (response.data.code) {
            case 'SUCCESS':
                return response.data.data;
            case 'BIZ.NO_LOGIN':
                MessageBox.confirm(`${response.data.msg}`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        Router.replace({
                            path: '/',
                            query: {
                                redirect: Router.currentRoute.fullPath
                            }
                        });
                    })
                    .catch(() => {});
                break;
            case 'BIZ.SESSION_NULL':
                if (sessionTipsCounter === 0) {
                    MessageBox.confirm(`${response.data.msg}`, '登录失效提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            Router.replace({
                                path: '/login',
                                query: {
                                    redirect: Router.currentRoute.fullPath
                                }
                            });
                        })
                        .catch(() => {});
                    sessionTipsCounter++;
                    const timeout = setTimeout(() => {
                        sessionTipsCounter = 0;
                        clearTimeout(timeout);
                    }, 1500);
                }
                break;
            default:
                Message.error(response.data.msg);
                return Promise.reject(response.data);
        }
    },
    error => {
        // if(!error.response){
        //     return false
        // }else {
        //     console.log(error.response.status)
        //     //自定义404等错误跳转页面，自己根据页面路由去修改
        //     switch (error.response.status){
        //         case 404:
        //             router.push({path: "/404"})
        //             break;
        //         case 500:
        //             alert('服务器异常!')
        //             break;
        //     }
        // }
        //
        return Promise.reject(error);
    }
);

export default instance;
